package me.leiusual.sharing.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 * Create by malei on 2018/6/28
 */
class QRCodeFinderView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

  private val paint = Paint()

  private val path = Path()

  private val density = context.resources.displayMetrics.density

  private val textRect = Rect()

  override fun onDraw(canvas: Canvas) {
    val width = measuredWidth.toFloat()
    val height = measuredHeight.toFloat()

    val finderWidth = width * 4 / 5
    val finderBottom = (height - finderWidth) / 2 + finderWidth

    path.apply {
      rewind()
      addRect(0f, 0f, width, height, Path.Direction.CW)
      addRect(
          (width - finderWidth) / 2,
          (height - finderWidth) / 2,
          (width - finderWidth) / 2 + finderWidth,
          finderBottom,
          Path.Direction.CCW
      )
    }

    paint.color = Color.parseColor("#66666666")
    canvas.drawPath(path, paint)

    paint.color = Color.WHITE
    paint.textSize = 14 * density
    paint.alpha = 0x40
    paint.typeface = Typeface.DEFAULT

    val text = "请扫邀请二维码完成注册"
    paint.getTextBounds(text, 0, text.length, textRect)
    canvas.drawText(text, (width - textRect.width()) / 2, finderBottom + textRect.height() * 2, paint)
  }
}